﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit.DramaDialog
{
    public partial class EditDramaSetPlayerBattleDialog : Form
    {
        PListDict curDict;
        public EditDramaSetPlayerBattleDialog()
        {
            InitializeComponent();
        }
        public void Show(IWin32Window owner, PListDict dict)
        {
            curDict = dict;
            PListDict dataDict = (PListDict)dict["DataDic"];
            listBoxBFIndex.Items.Clear();
            foreach (KeyValuePair<string,IPListElement> kv in dataDict)
            {
                listBoxBFIndex.Items.Add(kv.Key);
            }
            if (listBoxBFIndex.Items.Count > 0)
                listBoxBFIndex.SelectedIndex = 0;
            this.ShowDialog(owner);
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            long index = System.Int32.Parse(textBoxIndex.Text);
            try
            {
                PListDict dataDict = (PListDict)curDict["DataDic"];

                PListDict newBFIndex = new PListDict();
                newBFIndex.Add("BFIndex", new PListInteger(index));
                newBFIndex.Add("CoordX", new PListInteger(System.Int32.Parse(textBoxX.Text)));
                newBFIndex.Add("CoordY", new PListInteger(System.Int32.Parse(textBoxY.Text)));
                newBFIndex.Add("Direction", new PListInteger(comboBoxDirection.SelectedIndex));
                newBFIndex.Add("IsHidden", new PListBool(checkBoxIsHidden.Checked));
                dataDict.Add("BF" + index, newBFIndex);
                listBoxBFIndex.Items.Add("BF" + index);
                listBoxBFIndex.SelectedItem = "BF" + index;
            }
            catch
            {
                MessageBox.Show("你输入的数据有误");

            }
            

        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            string keyDeleted = listBoxBFIndex.SelectedItem.ToString();
            if (listBoxBFIndex.Items.Count > 0)
            {
                listBoxBFIndex.SelectedIndex = 0;
            }
            
            PListDict dataDict = (PListDict)curDict["DataDic"];
            dataDict.Remove(keyDeleted);
            listBoxBFIndex.Items.Remove(keyDeleted);
            
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            PListDict dataDict = (PListDict)curDict["DataDic"];
            string itemDeleted = listBoxBFIndex.SelectedItem.ToString();
            dataDict.Remove(itemDeleted);
            listBoxBFIndex.SelectedItem = null;
            listBoxBFIndex.Items.Remove(itemDeleted);
            long index = System.Int32.Parse(textBoxIndex.Text);
            PListDict newBFIndex = new PListDict();
            newBFIndex.Add("BFIndex", new PListInteger(index));
            newBFIndex.Add("CoordX", new PListInteger(System.Int32.Parse(textBoxX.Text)));
            newBFIndex.Add("CoordY", new PListInteger(System.Int32.Parse(textBoxY.Text)));
            newBFIndex.Add("Direction", new PListInteger(comboBoxDirection.SelectedIndex));
            newBFIndex.Add("IsHidden", new PListBool(checkBoxIsHidden.Checked));
            dataDict.Add("BF" + index, newBFIndex);
            listBoxBFIndex.Items.Add("BF" + index);
            listBoxBFIndex.SelectedItem = itemDeleted;
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            
            this.Hide();
        }

        private void listBoxBFIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxBFIndex.SelectedItem == null)
                return;
            PListDict dataDict = (PListDict)curDict["DataDic"];
            PListDict curBFIndex = (PListDict)dataDict[listBoxBFIndex.SelectedItem.ToString()];
            textBoxIndex.Text = ((PListInteger)curBFIndex["BFIndex"]).Value.ToString();
            textBoxX.Text = ((PListInteger)curBFIndex["CoordX"]).Value.ToString();
            textBoxY.Text = ((PListInteger)curBFIndex["CoordY"]).Value.ToString();
            comboBoxDirection.SelectedIndex = (int)((PListInteger)curBFIndex["Direction"]).Value;
            checkBoxIsHidden.Checked = (bool)((PListBool)curBFIndex["IsHidden"]).Value;
        }
    }
}
